package main

import (
	"fmt"
	"reflect"
)

type Tree struct {
	Id  int
	Age int
}

// Value - Interface
// Value - Type - Kind
// Value - Elem - Field - CanSet/CanAddr
func main() {
	t := Tree{1, 1024}
	v := reflect.ValueOf(&t)

	//
	i := v.Interface()
	fmt.Println(i)
	fmt.Println(t == *i.(*Tree)) //same
	fmt.Println("Type:", v.Type())
	fmt.Println("Type:", v.Type().Kind())
	//
	//fmt.Println("Len:", v.Len())
	//fmt.Println("Addr:", v.Addr())
	fmt.Println("CanAddr:", v.CanAddr())           //false why?
	fmt.Println("CanInterface:", v.CanInterface()) //true
	fmt.Println("CanSet:", v.CanSet())             //false

	//fmt.Println("v.NumField():", v.NumField()) //ERROR，因为用在指针上面
	fmt.Println("v.Elem().NumField():", v.Elem().NumField())
	for idx := 0; idx < v.Elem().NumField(); idx++ {
		field := v.Elem().Field(idx)
		fmt.Printf("field[%d]: %v\n", idx, field)
		if field.CanSet() {
			fmt.Println("can set!!!")
			field.Set(reflect.ValueOf(123))
		}
		if field.CanAddr() {
			fmt.Println("can addr!!!")
		}
	}
	fmt.Println(t)
}
